package com.qiku.web.servlet;

import com.alibaba.fastjson.JSON;
import com.qiku.base.BaseServlet;
import com.qiku.pojo.User;
import com.qiku.service.UserService;
import com.qiku.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    private User user = new User();

    /**
     * 根据用户名查询用户
     */
    public void findUserByName(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //获取请求参数
            String username = req.getParameter("username");
            //执行业务逻辑
            String userByName = userService.findUserByName(username);
            resp.getWriter().print(userByName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有用户
     */
    public void findAllUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String result = userService.findAllUser();
            resp.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


//    /**
//     * 用户注册Servlet层
//     */
//    public void userRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //设置文本格式
//        req.setCharacterEncoding("utf-8");
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        String is_root = req.getParameter("is_root");
//        user.setUsername(username);
//        user.setPassword(password);
//        if (is_root != null){
//            if (is_root.equals("1") || is_root.equals("0"))
//            user.setIs_root(Integer.parseInt(is_root));
//        }else{
//            user.setIs_root(0);
//        }
//        String str = userService.userRegister(user);
//        switch (str) {
//            case "success":
//                resp.sendRedirect("login.jsp");
//                break;
//            case "fail":
//                //返回提示信息
//                req.setAttribute("error", "用户注册失败！");
//                //获取转发器  并指定转发路径
//                req.getRequestDispatcher("register.jsp").forward(req, resp);
//                break;
//            case "msg":
//                //返回提示信息
//                req.setAttribute("error", "用户已存在！");
//                //获取转发器  并指定转发路径
//                req.getRequestDispatcher("register.jsp").forward(req, resp);
//            case "null":
//                //返回提示信息
//                req.setAttribute("error", "用户名和密码不能为空！");
//                //获取转发器  并指定转发路径
//                req.getRequestDispatcher("register.jsp").forward(req, resp);
//            default:
//                System.out.println("未知错误...");
//                break;
//        }
//    }

    /**
     * 管理员注销用户
     */
    public void destroyUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter respWriter = resp.getWriter();
            User user = (User) req.getSession().getAttribute("user");
            int is_root = user.getIs_root();
            Map<String,String> map = new HashMap<>();
            if (is_root == 1) {
                String id = req.getParameter("id");
                int i = userService.destroyUser(Integer.parseInt(id));
                if (i > 0) {
                    //管理员注销成功
                    map.put("destroyMsg","废弃成功！");
                } else {
                    //管理员注销失败
                    map.put("destroyMsg","废弃失败！");
                }
            } else {
                map.put("destroyMsg","无权操作！");
            }
            String result = JSON.toJSONString(map);
            respWriter.print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 管理员恢复被注销的用户
     *
     * @param req
     * @param resp
     */
    public void recoverUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            Map<String,String> map = new HashMap<>();
            int i = userService.recoverUser(Integer.parseInt(id));
            if (i > 0) {
                //管理员恢复成功
                map.put("destroyMsg","恢复成功！");
            } else {
                //管理员注销失败
                map.put("destroyMsg","恢复失败！");
            }
            String result = JSON.toJSONString(map);
            resp.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
